##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
## Engaged or Obedient?  Racially Differentiated Models of Democratic Education
## Replication - Citizenship Analysis 
## Inputs: citizen.csv
## Outputs: Data for Table 1, Table 2, D.1, and D.2. Produces Table D.3, Table D.4, Figure D.1, Figure D.2
##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##

rm(list=ls())

library(readr)
library(quanteda)
library(quanteda.corpora)
library(ggcorrplot)
library(ggplot2)
library(dplyr)
library(lubridate)
library(stats)
library(coefplot)
library(stargazer)
library(patchwork)

## Set your working directory
setwd("~/Desktop/TAD Handbooks Project/engaged_obedient_replication")

#Load coded citizenship data, re-name, drop excluded observations 
citizen <- read.csv("data/citizen.csv")
handbooks_all=citizen
handbooks_all=subset(handbooks_all, EXCLUDE==0)


#Add Race Dummy Variable (50/50)
handbooks_dummy <- handbooks_all%>%
  mutate(white_dummy = ifelse(white>=.5, 1, 0))

###Table 1 (rounded to the nearest whole number)
x <- handbooks_dummy[c("generic")]
table_xtabs <- xtabs(~generic, data=x)
table_xtabs
408/895

x <- handbooks_dummy[c("obedience")]
table_xtabs <- xtabs(~obedience, data=x)
table_xtabs
186/895

x <- handbooks_dummy[c("responsibility")]
table_xtabs <- xtabs(~responsibility, data=x)
table_xtabs
387/895

x <- handbooks_dummy[c("doing_good")]
table_xtabs <- xtabs(~doing_good, data=x)
table_xtabs
115/895

x <- handbooks_dummy[c("self_actualization")]
table_xtabs <- xtabs(~self_actualization, data=x)
table_xtabs
92/895

x <- handbooks_dummy[c("critical_thinking")]
table_xtabs <- xtabs(~critical_thinking, data=x)
table_xtabs
62/895

x <- handbooks_dummy[c("patriotism")]
table_xtabs <- xtabs(~patriotism, data=x)
table_xtabs
53/895

x <- handbooks_dummy[c("productive")]
table_xtabs <- xtabs(~productive, data=x)
table_xtabs
145/895

x <- handbooks_dummy[c("classical")]
table_xtabs <- xtabs(~classical, data=x)
table_xtabs
48/895

x <- handbooks_dummy[c("caring")]
table_xtabs <- xtabs(~caring, data=x)
table_xtabs
102/895

x <- handbooks_dummy[c("global")]
table_xtabs <- xtabs(~global, data=x)
table_xtabs
145/895

x <- handbooks_dummy[c("knowledgeable")]
table_xtabs <- xtabs(~knowledgeable, data=x)
table_xtabs
155/895

# Raw Statistical Data- Reported in Table 2 (and Appendix D Table D.1)
generic <- handbooks_dummy[c("generic", "white_dummy")]
generic <- na.omit(generic)
prop_generic<-generic%>%
  group_by(white_dummy)%>%
  summarise(prop_generic=sum(generic/n()))

obedience <- handbooks_dummy[c("obedience", "white_dummy")]
obedience <- na.omit(obedience)
prop_obedience<-obedience%>%
  group_by(white_dummy)%>%
  summarise(prop_obedience=sum(obedience/n()))

responsibility <- handbooks_dummy[c("responsibility", "white_dummy")]
responsibility <- na.omit(responsibility)
prop_responsibility<-responsibility%>%
  group_by(white_dummy)%>%
  summarise(prop_responsibility=sum(responsibility/n()))

doing_good <- handbooks_dummy[c("doing_good", "white_dummy")]
doing_good <- na.omit(doing_good)
prop_doing_good<-doing_good%>%
  group_by(white_dummy)%>%
  summarise(prop_doing_good=sum(doing_good/n()))

self_actualization <- handbooks_dummy[c("self_actualization", "white_dummy")]
self_actualization <- na.omit(self_actualization)
prop_self_actualization<-self_actualization%>%
  group_by(white_dummy)%>%
  summarise(prop_self_actualization=sum(self_actualization/n()))

critical_thinking <- handbooks_dummy[c("critical_thinking", "white_dummy")]
critical_thinking <- na.omit(critical_thinking)
prop_critical_thinking<-critical_thinking%>%
  group_by(white_dummy)%>%
  summarise(prop_critical_thinking=sum(critical_thinking/n()))

patriotism <- handbooks_dummy[c("patriotism", "white_dummy")]
patriotism <- na.omit(patriotism)
prop_patriotism<-patriotism%>%
  group_by(white_dummy)%>%
  summarise(prop_patriotism=sum(patriotism/n()))

productive <- handbooks_dummy[c("productive", "white_dummy")]
productive <- na.omit(productive)
prop_productive<-productive%>%
  group_by(white_dummy)%>%
  summarise(prop_productive=sum(productive/n()))

classical <- handbooks_dummy[c("classical", "white_dummy")]
classical <- na.omit(classical)
prop_classical<-classical%>%
  group_by(white_dummy)%>%
  summarise(prop_classical=sum(classical/n()))

caring <- handbooks_dummy[c("caring", "white_dummy")]
caring <- na.omit(caring)
prop_caring<-caring%>%
  group_by(white_dummy)%>%
  summarise(prop_caring=sum(caring/n()))

global <- handbooks_dummy[c("global", "white_dummy")]
global <- na.omit(global)
prop_global<-global%>%
  group_by(white_dummy)%>%
  summarise(prop_global=sum(global/n()))

knowledgeable <- handbooks_dummy[c("knowledgeable", "white_dummy")]
knowledgeable <- na.omit(knowledgeable)
prop_knowledgeable<-knowledgeable%>%
  group_by(white_dummy)%>%
  summarise(prop_knowledgeable=sum(knowledgeable/n()))


#Difference in Means Test 
handbooks_dummy$White<-handbooks_dummy$white_dummy
Generic <- lm(generic~White, data=handbooks_dummy)
summary(Generic)
Obedience<-lm(obedience~White, data=handbooks_dummy)
summary(Obedience)
Responsibility<-lm(responsibility~White, data=handbooks_dummy)
summary(Responsibility)
Doing_Good <-lm(doing_good~White, data=handbooks_dummy)
summary(Doing_Good)
Self_Actualization <-lm(self_actualization~White, data=handbooks_dummy)
summary(Self_Actualization)
Critical_Thinking <-lm(critical_thinking~White, data=handbooks_dummy)
summary(Critical_Thinking)
Patriotism <-lm(patriotism~White, data=handbooks_dummy)
summary(Patriotism)
Productive<-lm(productive~White, data=handbooks_dummy)
summary(Productive)
Classical<-lm(classical~White, data=handbooks_dummy)
summary(Classical)
Caring<-lm(caring~White, data=handbooks_dummy)
summary(Caring)
Global<-lm(global~White, data=handbooks_dummy)
summary(Global)
Knowledgeable<-lm(knowledgeable~White, data=handbooks_dummy)
summary(Knowledgeable)


# Appendix D, Figure 1
p1<- coefplot(Generic, intercept=FALSE, ylab=" ", title="Generic",
              zeroColor = "grey", fillColor="white")

p2<- coefplot(Obedience, intercept=FALSE, ylab=" ", title="Obedience",
              zeroColor = "grey", fillColor="white")

p3<- coefplot(Responsibility, intercept=FALSE, ylab=" ", title="Responsibility",
              zeroColor = "grey", fillColor="white")

p4<- coefplot(Doing_Good, intercept=FALSE, ylab=" ", title="Doing Good",
              zeroColor = "grey", fillColor="white")

p5<- coefplot(Self_Actualization, intercept=FALSE, ylab=" ", title="Self Actualization",
              zeroColor = "grey", fillColor="white")

p6 <- coefplot(Critical_Thinking, intercept=FALSE, ylab=" ", title="Critical Thinking",
               zeroColor = "grey", fillColor="white")

p7<- coefplot(Patriotism, intercept=FALSE, ylab=" ", title="Patriotism",
              zeroColor = "grey", fillColor="white")

p8<- coefplot(Generic, intercept=FALSE, ylab=" ", title="Productive",
              zeroColor = "grey", fillColor="white")

p9<- coefplot(Classical, intercept=FALSE, ylab=" ", title="Classical",
              zeroColor = "grey", fillColor="white")

p10<- coefplot(Caring, intercept=FALSE, ylab=" ", title="Caring",
               zeroColor = "grey", fillColor="white")

p11<- coefplot(Global, intercept=FALSE, ylab=" ", title="Global",
               zeroColor = "grey", fillColor="white")

p12<- coefplot(Knowledgeable, intercept=FALSE, ylab= " ", title="Knowledgeable",
               zeroColor = "grey", fillColor="white")

(p1 | p2 | p3 ) /
  (p4 | p5 | p6) /
  (p7 | p8 | p9) /
  (p10 | p11 | p12) 

ggsave("figures/engaged_obedient_D_figure_1.pdf")
ggsave("figures/engaged_obedient_D_figure_1.jpeg")


# Appendix- Table D.3
stargazer(Generic, Obedience, Responsibility, Doing_Good, Self_Actualization, Critical_Thinking,
          Patriotism, Productive, Classical, Caring, Global, Knowledgeable, type="text", out="figures/regression.txt", 
          title="Normative Conceptions of Citizens by Race")

stargazer(Generic, Obedience, Responsibility, Doing_Good, Self_Actualization, Critical_Thinking,
          Patriotism, Productive, Classical, Caring, Global, Knowledgeable, type="html", out="figures/regression.html", 
          title="Normative Conceptions of Citizens by Race")


#### Analysis of Racial Differences- 70/30

rm(list=ls())

#Re-read in data, rename, drop excluded observations
citizen <- read.csv("data/citizen.csv")
handbooks_all=citizen
handbooks_all=subset(handbooks_all, EXCLUDE==0)

#Subset by race and add dummy
maj_white <- subset(handbooks_all, white>=.70001)
maj_white$white_dummy <-"1"

maj_non_white <- subset(handbooks_all, white<=.29999)
maj_non_white$white_dummy <-"0"

#remerge data
handbooks_dummy <- rbind (maj_white, maj_non_white)



# Raw Statistical Data- Reported in Appendix D Table D.2
generic <- handbooks_dummy[c("generic", "white_dummy")]
generic <- na.omit(generic)
prop_generic<-generic%>%
  group_by(white_dummy)%>%
  summarise(prop_generic=sum(generic/n()))

obedience <- handbooks_dummy[c("obedience", "white_dummy")]
obedience <- na.omit(obedience)
prop_obedience<-obedience%>%
  group_by(white_dummy)%>%
  summarise(prop_obedience=sum(obedience/n()))

responsibility <- handbooks_dummy[c("responsibility", "white_dummy")]
responsibility <- na.omit(responsibility)
prop_responsibility<-responsibility%>%
  group_by(white_dummy)%>%
  summarise(prop_responsibility=sum(responsibility/n()))

doing_good <- handbooks_dummy[c("doing_good", "white_dummy")]
doing_good <- na.omit(doing_good)
prop_doing_good<-doing_good%>%
  group_by(white_dummy)%>%
  summarise(prop_doing_good=sum(doing_good/n()))

self_actualization <- handbooks_dummy[c("self_actualization", "white_dummy")]
self_actualization <- na.omit(self_actualization)
prop_self_actualization<-self_actualization%>%
  group_by(white_dummy)%>%
  summarise(prop_self_actualization=sum(self_actualization/n()))

critical_thinking <- handbooks_dummy[c("critical_thinking", "white_dummy")]
critical_thinking <- na.omit(critical_thinking)
prop_critical_thinking<-critical_thinking%>%
  group_by(white_dummy)%>%
  summarise(prop_critical_thinking=sum(critical_thinking/n()))

patriotism <- handbooks_dummy[c("patriotism", "white_dummy")]
patriotism <- na.omit(patriotism)
prop_patriotism<-patriotism%>%
  group_by(white_dummy)%>%
  summarise(prop_patriotism=sum(patriotism/n()))

productive <- handbooks_dummy[c("productive", "white_dummy")]
productive <- na.omit(productive)
prop_productive<-productive%>%
  group_by(white_dummy)%>%
  summarise(prop_productive=sum(productive/n()))

classical <- handbooks_dummy[c("classical", "white_dummy")]
classical <- na.omit(classical)
prop_classical<-classical%>%
  group_by(white_dummy)%>%
  summarise(prop_classical=sum(classical/n()))

caring <- handbooks_dummy[c("caring", "white_dummy")]
caring <- na.omit(caring)
prop_caring<-caring%>%
  group_by(white_dummy)%>%
  summarise(prop_caring=sum(caring/n()))

global <- handbooks_dummy[c("global", "white_dummy")]
global <- na.omit(global)
prop_global<-global%>%
  group_by(white_dummy)%>%
  summarise(prop_global=sum(global/n()))

knowledgeable <- handbooks_dummy[c("knowledgeable", "white_dummy")]
knowledgeable <- na.omit(knowledgeable)
prop_knowledgeable<-knowledgeable%>%
  group_by(white_dummy)%>%
  summarise(prop_knowledgeable=sum(knowledgeable/n()))


#Difference in Means Test for Variables of Interest
handbooks_dummy$White<-handbooks_dummy$white_dummy
Generic <- lm(generic~White, data=handbooks_dummy)
summary(Generic)
Obedience<-lm(obedience~White, data=handbooks_dummy)
summary(Obedience)
Responsibility<-lm(responsibility~White, data=handbooks_dummy)
summary(Responsibility)
Doing_Good <-lm(doing_good~White, data=handbooks_dummy)
summary(Doing_Good)
Self_Actualization <-lm(self_actualization~White, data=handbooks_dummy)
summary(Self_Actualization)
Critical_Thinking <-lm(critical_thinking~White, data=handbooks_dummy)
summary(Critical_Thinking)
Patriotism <-lm(patriotism~White, data=handbooks_dummy)
summary(Patriotism)
Productive<-lm(productive~White, data=handbooks_dummy)
summary(Productive)
Classical<-lm(classical~White, data=handbooks_dummy)
summary(Classical)
Caring<-lm(caring~White, data=handbooks_dummy)
summary(Caring)
Global<-lm(global~White, data=handbooks_dummy)
summary(Global)
Knowledgeable<-lm(knowledgeable~White, data=handbooks_dummy)
summary(Knowledgeable)



#Appendix D, Figure 2
p1<- coefplot(Generic, intercept=FALSE, ylab=" ", title="Generic",
              zeroColor = "grey", fillColor="white")

p2<- coefplot(Obedience, intercept=FALSE, ylab=" ", title="Obedience",
              zeroColor = "grey", fillColor="white")

p3<- coefplot(Responsibility, intercept=FALSE, ylab=" ", title="Responsibility",
              zeroColor = "grey", fillColor="white")

p4<- coefplot(Doing_Good, intercept=FALSE, ylab=" ", title="Doing Good",
              zeroColor = "grey", fillColor="white")

p5<- coefplot(Self_Actualization, intercept=FALSE, ylab=" ", title="Self Actualization",
              zeroColor = "grey", fillColor="white")

p6 <- coefplot(Critical_Thinking, intercept=FALSE, ylab=" ", title="Critical Thinking",
               zeroColor = "grey", fillColor="white")

p7<- coefplot(Patriotism, intercept=FALSE, ylab=" ", title="Patriotism",
              zeroColor = "grey", fillColor="white")

p8<- coefplot(Generic, intercept=FALSE, ylab=" ", title="Productive",
              zeroColor = "grey", fillColor="white")

p9<- coefplot(Classical, intercept=FALSE, ylab=" ", title="Classical",
              zeroColor = "grey", fillColor="white")

p10<- coefplot(Caring, intercept=FALSE, ylab=" ", title="Caring",
               zeroColor = "grey", fillColor="white")

p11<- coefplot(Global, intercept=FALSE, ylab=" ", title="Global",
               zeroColor = "grey", fillColor="white")

p12<- coefplot(Knowledgeable, intercept=FALSE, ylab= " ", title="Knowledgeable",
               zeroColor = "grey", fillColor="white")

(p1 | p2 | p3 ) /
  (p4 | p5 | p6) /
  (p7 | p8 | p9) /
  (p10 | p11 | p12) 

ggsave("figures/engaged_obedient_D_figure_2.pdf")
ggsave("figures/engaged_obedient_D_figure_2.jpeg")

#Table D.4
stargazer(Generic, Obedience, Responsibility, Doing_Good, Self_Actualization, Critical_Thinking,
          Patriotism, Productive, Classical, Caring, Global, Knowledgeable, type="html", out="figures/regression_2.html", 
          title="Normative Conceptions of Citizens by Race")

stargazer(Generic, Obedience, Responsibility, Doing_Good, Self_Actualization, Critical_Thinking,
          Patriotism, Productive, Classical, Caring, Global, Knowledgeable, type="text", out="figures/regression_2.txt", 
          title="Normative Conceptions of Citizens by Race")

